package Lib3d.PresetModel3d;


import Lib3d.Vector3d;
import Lib3d.MyUtils.Debug;

import java.awt.Color;
import java.util.Random;
import java.util.Vector;

import javax.media.opengl.GLAutoDrawable;

/**
 * A hairy sphere 3d model implementation.
 * 
 * @author Wassim Chegham
 * @version 0.1
 * @category Lib3d.PresetModel3d
 * 
 */
public class HairySphere extends Sphere {

    private int numberOfhairs;
    private final Vector<Hair> hairList;

    private final float radius;
    private final Color hairColor;

    public HairySphere(final Vector3d sphereCenter, final float radius,
            final int numberOfHairs) {
        this(sphereCenter, radius, numberOfHairs, Color.GREEN, Color.RED);
    }

    public HairySphere(final Vector3d sphereCenter, final float radius,
            final int numberOfHairs, final Color sphereColor, final Color hairColor) {
        super(sphereCenter, radius);
        super.setSphereColor(sphereColor);
        this.hairList = new Vector<Hair>();
        this.radius = radius;
        this.numberOfhairs = numberOfHairs;
        this.hairColor = hairColor;

        // this.generateSequentialHairs();
        this.generateRandomHairs();
    }

    @Override
    public void draw(final GLAutoDrawable gLDrawable) {
        super.draw(gLDrawable);
        if (Debug.showHairs) {
            for (final Hair h : this.hairList) {
                h.animate().draw(gLDrawable);
            }
        }
    }

    public int getNumberOfhairs() {
        return this.numberOfhairs;
    }

    public void setNumberOfhairs(final int numberOfhairs) {
        this.numberOfhairs = numberOfhairs;
    }

    private void generateRandomHairs() {
        Hair h;
        final Random rand = new Random(System.currentTimeMillis());
        for (int i = 0; i < this.numberOfhairs; i++) {
            final float theta = (float) (rand.nextFloat() * (2 * Math.PI) - Math.PI);
            final float phi = (float) (rand.nextFloat() * (2 * Math.PI) - Math.PI);

            final float x = (float) (this.radius * Math.sin(theta) * Math.cos(phi));
            final float y = (float) (this.radius * Math.sin(theta) * Math.sin(phi));
            final float z = (float) (this.radius * Math.cos(theta));

            final Vector3d randVector = new Vector3d(x, y, z);

            h = new Hair(randVector, theta, phi, this.hairColor);

            this.hairList.add(h);

        }
    }

    private void generateSequentialHairs() {
        Hair h;
        for (int anglez = 1; anglez <= this.numberOfhairs / 5; anglez++) {
            for (int phi = 1; phi <= this.numberOfhairs / 5; phi++) {
                for (int theta = 1; theta <= this.numberOfhairs / 5; theta++) {
                    final float x = this.radius * (float) Math.sin(theta)
                            * (float) Math.cos(phi);
                    final float y = this.radius * (float) Math.sin(theta)
                            * (float) Math.sin(phi);
                    final float z = this.radius * (float) Math.cos(theta);
                    h = new Hair(new Vector3d(x, y, z), theta, phi, this.hairColor);
                    this.hairList.add(h);
                }
            }
        }

    }
}
